package org.valire.usermanagement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.valire.usermanagement.beans.Role;
import org.valire.usermanagement.beans.User;
import org.valire.usermanagement.services.RolesService;

public class UmDtoConversions {

	private static RolesService rolesService;
	
	public void setRolesService(RolesService rolesService) {
		UmDtoConversions.rolesService = rolesService;
	}

	/**
	 * 
	 * @param roleDto
	 * @return
	 */
	public static Role convert(RoleDto roleDto) {
		if(roleDto == null)
			return null;
		Role role = rolesService.getRole(roleDto.getRoleName());
		return role;
	}

	public static RoleDto convert(Role role) {
		if(role == null)
			return null;
		RoleDto roleDto = new RoleDto();
		roleDto.setId(role.getId());
		roleDto.setCreationTime(role.getCreationTime());
		roleDto.setRoleName(role.getRoleName());
		return roleDto;
	}

	public static List<RoleDto> convert(List<Role> roles) {
		List<RoleDto> l = new ArrayList<RoleDto>();
		for (Role role : roles) {
			l.add(convert(role));
		}
		return l;
	}
	public static Set<Role> convertDtos(Collection<RoleDto> roleDtos) {
		Set<Role> st = new HashSet<Role>();
		for (RoleDto roleDto : roleDtos) {
			st.add(convert(roleDto));
		}
		return st;
	}
	
	public static UserDto convert(User user) {
		if(user == null)
			return null;
		UserDto dto = new UserDto();
		dto.setEmail(user.getEmail());
		dto.setAccountNonExpired(user.isAccountNonExpired());
		dto.setAccountNonLocked(user.isAccountNonLocked());
		dto.setEnabled(user.isEnabled());
		dto.setCreationTime(user.getCreationTime());
		dto.setSapLanguage(user.getSapLanguage());
		dto.setSapName(user.getSapName());
		dto.setLastName(user.getLastName());
		dto.setFirstName(user.getFirstName());
		dto.setUserName(user.getUserName());
		dto.setId(user.getId());
		
		
		Set<Role> roles = user.getRoles();
		Set<RoleDto> rolesDto = new HashSet<RoleDto>();
		for (Role role : roles) {
			RoleDto roleDto = new RoleDto();
			roleDto.setId(role.getId());
			roleDto.setRoleName(role.getRoleName());
			roleDto.setCreationTime(role.getCreationTime());
			rolesDto.add(roleDto);
		}
		dto.setRoles(rolesDto);
 		return dto;
	}

	public static User convert(UserDto dto) {
		if(dto == null)
			return null;
		User u = new User();
		u.setEmail(dto.getEmail());
		u.setAccountNonExpired(dto.isAccountNonExpired());
		u.setAccountNonLocked(dto.isAccountNonLocked());
		u.setEnabled(dto.isEnabled());
		u.setCreationTime(dto.getCreationTime());
		u.setSapLanguage(dto.getSapLanguage());
		u.setSapName(dto.getSapName());
		u.setLastName(dto.getLastName());
		u.setFirstName(dto.getFirstName());
		u.setUserName(dto.getUserName());
		u.setId(dto.getId());
		u.setPassword(dto.getPassword());
		u.setCredentialsNoneExpired(dto.getCredentialsNoneExpired());
		u.setConfigurations(dto.getConfigurations());
		
		Set<Role> roles = convertDtos(dto.getRoles());
		u.setRoles(roles);
		return u;
	}
}
